<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Cloud 负载均衡测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
            font-size: 14px;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .stats {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9ecef;
            border-radius: 4px;
        }
        .service-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Spring Cloud 负载均衡测试</h1>
        
        <div class="service-info">
            <div>
                <strong>Consumer Service:</strong> localhost:8090
            </div>
            <div>
                <strong>Provider Services:</strong> 8081, 8082, 8083
            </div>
        </div>

        <div>
            <button class="button" onclick="testLoadBalance()">测试负载均衡</button>
            <button class="button" onclick="getProviderInfo()">获取服务信息</button>
            <button class="button" onclick="clearResults()">清空结果</button>
            <button class="button" onclick="runMultipleTests()">连续测试 (10次)</button>
        </div>

        <div class="stats" id="stats">
            <h3>统计信息</h3>
            <div id="statsContent">点击测试按钮开始统计</div>
        </div>

        <div class="result" id="result">等待测试...</div>
    </div>

    <script>
        let stats = {
            '8081': 0,
            '8082': 0,
            '8083': 0,
            total: 0
        };

        function updateStats(port) {
            if (stats[port] !== undefined) {
                stats[port]++;
            }
            stats.total++;
            
            const statsContent = document.getElementById('statsContent');
            statsContent.innerHTML = `
                <div>总请求数: ${stats.total}</div>
                <div>端口 8081: ${stats['8081']} 次 (${((stats['8081']/stats.total)*100).toFixed(1)}%)</div>
                <div>端口 8082: ${stats['8082']} 次 (${((stats['8082']/stats.total)*100).toFixed(1)}%)</div>
                <div>端口 8083: ${stats['8083']} 次 (${((stats['8083']/stats.total)*100).toFixed(1)}%)</div>
            `;
        }

        function testLoadBalance() {
            fetch('/api/consumer/test-load-balance')
                .then(response => response.json())
                .then(data => {
                    const result = document.getElementById('result');
                    result.textContent = JSON.stringify(data, null, 2);
                    
                    // 提取端口信息
                    if (data.providerResponse && data.providerResponse.serverPort) {
                        updateStats(data.providerResponse.serverPort);
                    }
                })
                .catch(error => {
                    document.getElementById('result').textContent = '错误: ' + error.message;
                });
        }

        function getProviderInfo() {
            fetch('/api/consumer/provider-info')
                .then(response => response.json())
                .then(data => {
                    const result = document.getElementById('result');
                    result.textContent = JSON.stringify(data, null, 2);
                })
                .catch(error => {
                    document.getElementById('result').textContent = '错误: ' + error.message;
                });
        }

        function clearResults() {
            document.getElementById('result').textContent = '等待测试...';
            stats = {
                '8081': 0,
                '8082': 0,
                '8083': 0,
                total: 0
            };
            document.getElementById('statsContent').textContent = '点击测试按钮开始统计';
        }

        function runMultipleTests() {
            clearResults();
            let count = 0;
            const maxCount = 10;
            
            function runTest() {
                if (count < maxCount) {
                    testLoadBalance();
                    count++;
                    setTimeout(runTest, 500); // 每500ms执行一次
                }
            }
            
            runTest();
        }
    </script>
</body>
</html> 